package org.jeecg.modules.flow.callback.inbound.other;

import cn.hutool.core.util.StrUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.transfer.entity.PaperPurchase;
import org.jeecg.modules.business.transfer.service.IPaperPurchaseService;
import org.jeecg.modules.business.transfer.vo.PaperPurchasePage;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 纸筒采购任务提交回调前bean
 *
 * @author lumingyi
 * @version 1.0.0
 * @since 2023-11-14
 */
@Component
public class R47PaperPurchaseSubmitBeforeBean<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IPaperPurchaseService paperPurchaseService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        //保存
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        baseFlowDto.translateClass(PaperPurchasePage.class);
        PaperPurchasePage data = (PaperPurchasePage) baseFlowDto.getData();
        PaperPurchase paperPurchase = new PaperPurchase();
        BeanUtils.copyProperties(data, paperPurchase);
        String id = null;
        if (StrUtil.isBlank(data.getId())) {
            id = paperPurchaseService.saveMain(paperPurchase, data.getWmsPaperPurchaseMaterialList());
            baseFlowDto.setInfoId(id);
        } else {
            id = data.getId();
        }
        // 提交
        PaperPurchase po = paperPurchaseService.getById(id);
        po.setDocumentStatus("2");  // 任务提交之后为未发送
        paperPurchaseService.updateById(po);
        baseFlowDto.setResult(Result.OK("提交成功"));
        baseFlowDto.setInfoId(id);
    }
}
